package process

import (
	"encoding/json"
	"fmt"
	clientmodel "helloword01/31chatroom/client/model"
	commonmodel "helloword01/31chatroom/common/model"
	"helloword01/31chatroom/common/utils"
	"time"
)

type SmsProcess struct {
}

// SendGroupMes
//
//	@Description: 发送群消息
//	@receiver this
//	@param content
//	@return err
func (this *SmsProcess) SendGroupMes(content string) (err error) {
	//构建群消息
	var smsMes commonmodel.SmsMes
	smsMes.Content = content
	smsMes.UserId = clientmodel.CUR_USER.UserId
	smsMes.UserStatus = clientmodel.CUR_USER.UserStatus

	data, err := json.Marshal(smsMes)
	if err != nil {
		fmt.Println(commonmodel.SerializeJsonError, err.Error())
		return
	}
	data, err = utils.BuildMessage(data, commonmodel.SmsMesType)
	if err != nil {
		fmt.Println("BuildMessage fail =", err.Error())
		return
	}
	tf := &utils.Transfer{
		Conn: clientmodel.CUR_USER.Conn,
	}
	return tf.WritePkg(data)
}

func (this *SmsProcess) SendPrivateMes(content string) (err error) {
	toUserId, toUserContent, err := utils.ParsePrivateMes(content)
	if err != nil {
		fmt.Println(commonmodel.ParseJsonError, err.Error())
		return
	}
	var smsMes commonmodel.SmsPrivateMes
	smsMes.Content = toUserContent
	smsMes.UserId = clientmodel.CUR_USER.UserId
	smsMes.UserStatus = clientmodel.CUR_USER.UserStatus
	smsMes.ToUserId = toUserId
	data, err := json.Marshal(smsMes)
	if err != nil {
		fmt.Println(commonmodel.SerializeJsonError, err.Error())
		return
	}
	data, err = utils.BuildMessage(data, commonmodel.SmsPrivateMesType)
	if err != nil {
		fmt.Println("BuildMessage fail =", err.Error())
		return
	}
	tf := &utils.Transfer{
		Conn: clientmodel.CUR_USER.Conn,
	}
	return tf.WritePkg(data)
}

// LogoutMes
//
//	@Description: 发送下线消息
//	@receiver this
//	@return err
func (this *SmsProcess) SendLogoutMes() (err error) {
	var logoutMes commonmodel.LogoutMes
	logoutMes.UserId = clientmodel.CUR_USER.UserId
	logoutMes.LogoutTime = time.Now().Format("2006-01-02 15:04:05")

	data, err := json.Marshal(logoutMes)
	if err != nil {
		fmt.Println(commonmodel.SerializeJsonError, err.Error())
		return
	}
	data, err = utils.BuildMessage(data, commonmodel.LogoutMesType)
	if err != nil {
		fmt.Println("BuildMessage fail =", err.Error())
		return
	}
	tf := &utils.Transfer{
		Conn: clientmodel.CUR_USER.Conn,
	}
	return tf.WritePkg(data)
}
